package com.ctshk.rpc.order.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.enums.hotel.HotelHasBreakfast;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.hotel.dto.HotelProductDTO;
import com.ctshk.rpc.hotel.service.IHotelOrderReceiptService;
import com.ctshk.rpc.hotel.service.IHotelProductService;
import com.ctshk.rpc.order.hotel.dto.HotelOrderCollectionReceiptGetDTO;
import com.ctshk.rpc.order.hotel.dto.HotelOrderProductReceiptGetDTO;
import com.ctshk.rpc.order.hotel.dto.HotelOrderReceiptGetDTO;
import com.ctshk.rpc.order.hotel.entity.HotelOrder;
import com.ctshk.rpc.order.hotel.entity.HotelOrderCollection;
import com.ctshk.rpc.order.hotel.entity.HotelOrderReceipt;
import com.ctshk.rpc.order.hotel.mapper.HotelOrderCollectionMapper;
import com.ctshk.rpc.order.hotel.mapper.HotelOrderMapper;
import com.ctshk.rpc.order.hotel.mapper.HotelOrderReceiptMapper;
import com.ctshk.rpc.order.hotel.mapper.HotelOrderTouristMapper;
import com.ctshk.rpc.order.hotel.req.HotelOrderReceiptSaveReq;
import com.ctshk.rpc.system.service.IFileOSSService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 酒店订单收据 服务实现类
 * </p>
 *
 * @since 2021-04-15
 */
@DubboService
public class HotelOrderReceiptServiceImpl extends ServiceImpl<HotelOrderReceiptMapper, HotelOrderReceipt> implements IHotelOrderReceiptService {
    @Autowired
    private HotelOrderReceiptMapper hotelOrderReceiptMapper;
    @Autowired
    private HotelOrderMapper hotelOrderMapper;
    @Autowired
    private HotelOrderCollectionMapper hotelOrderCollectionMapper;
    @Autowired
    private HotelOrderTouristMapper hotelOrderTouristMapper;
    @DubboReference
    private IHotelProductService hotelProductService;
    @DubboReference
    private IFileOSSService fileOSSService;

    @Override
    public int update(HotelOrderReceiptSaveReq req, Long userId) {
        QueryWrapper<HotelOrderReceipt> receiptQueryWrapper = new QueryWrapper<>();
        receiptQueryWrapper.lambda().eq(HotelOrderReceipt::getOrderId, req.getOrderId());
        HotelOrderReceipt airportExpressOrderReceiptTemp = hotelOrderReceiptMapper.selectOne(receiptQueryWrapper);
        if (airportExpressOrderReceiptTemp == null) {
            //该订单不存在收据
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60026);
        }
        HotelOrderReceipt airportExpressOrderReceipt = EntityUtil.copy(req, HotelOrderReceipt.class);
        UpdateWrapper<HotelOrderReceipt> receiptUpdateWrapper = new UpdateWrapper<>();
        receiptUpdateWrapper.lambda().eq(HotelOrderReceipt::getOrderId, airportExpressOrderReceipt.getOrderId());
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getGuest, airportExpressOrderReceipt.getGuest());
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getPhone, airportExpressOrderReceipt.getPhone());
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getAttention, airportExpressOrderReceipt.getAttention());
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getRemark, airportExpressOrderReceipt.getRemark());
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getModifiedId, userId);
        receiptUpdateWrapper.lambda().set(HotelOrderReceipt::getGmtModified, LocalDateTime.now());
        int updateNum = hotelOrderReceiptMapper.update(null, receiptUpdateWrapper);
        return updateNum;
    }

    @Override
    public HotelOrderReceiptGetDTO getReceiptByOrderId(Long orderId) {
        HotelOrder hotelOrder = hotelOrderMapper.selectById(orderId);
        if (hotelOrder == null) {
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60002);
        }
        QueryWrapper<HotelOrderReceipt> receiptQueryWrapper = new QueryWrapper<>();
        receiptQueryWrapper.lambda().eq(HotelOrderReceipt::getOrderId, orderId);
        HotelOrderReceipt hotelOrderReceiptTemp = hotelOrderReceiptMapper.selectOne(receiptQueryWrapper);
        if (hotelOrder == null) {
            //该订单不存在收据
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60026);
        }

        /**
         * info
         */
        HotelOrderReceiptGetDTO hotelOrderReceiptGetDTO = EntityUtil.copy(hotelOrderReceiptTemp, HotelOrderReceiptGetDTO.class);
        hotelOrderReceiptGetDTO.setInquiryNumber(hotelOrder.getInquiryNumber());
        hotelOrderReceiptGetDTO.setReceiptNo(hotelOrderReceiptTemp.getReceiptNo());
        hotelOrderReceiptGetDTO.setGuest(hotelOrderReceiptTemp.getGuest());
        hotelOrderReceiptGetDTO.setPhone(hotelOrderReceiptTemp.getPhone());
        hotelOrderReceiptGetDTO.setOrderInfo(hotelOrder.getHotelProductName());

        /**
         * 内容Detail
         */
        ArrayList<HotelOrderProductReceiptGetDTO> hotelOrderProductReceiptGets = new ArrayList<>();
        hotelOrderReceiptGetDTO.setHotelOrderProductReceiptGets(hotelOrderProductReceiptGets);

        HotelOrderProductReceiptGetDTO hotelOrderProductReceiptGetDTO = new HotelOrderProductReceiptGetDTO();
        hotelOrderProductReceiptGetDTO.setHotelName(hotelOrder.getHotelName());
        hotelOrderProductReceiptGetDTO.setProductName(hotelOrder.getHotelProductName());
        hotelOrderProductReceiptGetDTO.setHasBreakfast(HotelHasBreakfast.parseValue(hotelOrder.getHasBreakfast()));
        hotelOrderProductReceiptGetDTO.setDate(hotelOrder.getCheckInDate() + " - " + hotelOrder.getCheckOutDate());
        hotelOrderProductReceiptGetDTO.setRoomNum(hotelOrder.getNumber());
        hotelOrderProductReceiptGetDTO.setNights(hotelOrder.getNights());
        hotelOrderProductReceiptGets.add(hotelOrderProductReceiptGetDTO);

        /**
         * 支付详情
         */
        hotelOrderReceiptGetDTO.setOrderAmount(hotelOrder.getOrderAmount());
        hotelOrderReceiptGetDTO.setReceivableAmount(hotelOrder.getReceivableAmount());
        QueryWrapper<HotelOrderCollection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.lambda().eq(HotelOrderCollection::getOrderId, orderId);
        collectionQueryWrapper.lambda().eq(HotelOrderCollection::getIsDeleted, IsDeletedCode.NO.getCode());
        List<HotelOrderCollection> hotelOrderCollections = hotelOrderCollectionMapper.selectList(collectionQueryWrapper);
        List<HotelOrderCollectionReceiptGetDTO> hotelOrderCollectionReceiptGets = EntityUtil.copyList(hotelOrderCollections, HotelOrderCollectionReceiptGetDTO.class);
        hotelOrderReceiptGetDTO.setHotelOrderCollectionReceiptGets(hotelOrderCollectionReceiptGets);

        /**
         * 预订须知
         */
        HotelProductDTO queryDetail = hotelProductService.queryDetail(hotelOrder.getHotelProductId());
        hotelOrderReceiptGetDTO.setBookingInformation(queryDetail.getCustomerNotice());
        return hotelOrderReceiptGetDTO;
    }

}
